package com.sgb.trip.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.trip.api.TripNet;
import com.sgb.trip.entity.PassengerInfoEntity;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;

import java.util.Map;

/**
 * Time:2021/9/13
 * Author:黄淋
 * Description:新增和编辑联系人
 */
public class FlightAddPassengerModel extends BaseViewModel {
    public MutableLiveData<String> ldAddData = new MutableLiveData<>();
    public MutableLiveData<String> ldDeleteData = new MutableLiveData<>();


    public FlightAddPassengerModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 新增 编辑乘客
     */
    public void savePassenger(Map<String, Object> map) {
        TripNet.getInstance().getService().saveOrUpdatePassenger(map).enqueue(new ICallBack<BaseEntity<String>>() {
            @Override
            public void onSuccess(BaseEntity<String> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null || data.getData() == null) {
                    return;
                }

                if (!data.getState().equals(APIManager.RESPONSE_OK)) {
                    mToast.postValue(data.getMsg());
                    return;
                }
                if (data.getData() != null) {
                    ldAddData.postValue(data.getData());
                }
            }

            @Override
            public void onFailureWithEntity(BaseEntity entity) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 删除乘客
     */
    public void deletePassenger(String id) {
        TripNet.getInstance().getService().deletePassenger(id).enqueue(new ICallBack<BaseEntity<String>>() {
            @Override
            public void onSuccess(BaseEntity<String> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null || data.getData() == null) {
                    return;
                }

                if (data.getData() != null) {
                    ldDeleteData.postValue(data.getData());
                }
            }

            @Override
            public void onFailureWithEntity(BaseEntity entity) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
